package it.side.messaging.client;

import java.io.Serializable;
import java.util.Arrays;

import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONNumber;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONString;
import com.google.gwt.json.client.JSONValue;

public class Message implements Serializable {

	private static final long serialVersionUID = 1L;
	
	private Integer id;
	private String from;
	private String to;
	private String content;
	private String[] params = new String[0];
	
	public static final String BROADCAST_TO = "*";
	
	public Message() {
		super();
	}
	
	public Message(Integer id, String from, String to, String content) {
		super();
		this.id = id;
		this.from = from;
		this.to = to;
		this.content = content;
	}

	public String getFrom() {
		return from;
	}
	public void setFrom(String from) {
		this.from = from;
	}
	public String getTo() {
		return to;
	}
	public void setTo(String to) {
		this.to = to;
	}
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
	public String[] getParams() {
		return params;
	}
	public void setParams(String[] params) {
		this.params = params;
	}
	public boolean isBroadcast() {
		return BROADCAST_TO.equalsIgnoreCase(to);
	}

	public JSONObject toJSON() {
		JSONObject res = new JSONObject();
		res.put("id", new JSONNumber(id));
		res.put("from", new JSONString(from));
		res.put("to", new JSONString(to));
		res.put("content", new JSONString(content));
		JSONArray array = new JSONArray();
		for (int i = 0; i < params.length; i++) {
			array.set(i, new JSONString(params[i]));
		}
		res.put("params", array);
		return res;
	}
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((from == null) ? 0 : from.hashCode());
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Message other = (Message) obj;
		if (from == null) {
			if (other.from != null)
				return false;
		} else if (!from.equals(other.from))
			return false;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "Message [id=" + id + ", from=" + from + ", to=" + to
				+ ", content=" + content + ", params="
				+ Arrays.toString(params) + "]";
	}

	public static Message fromJSON(JSONValue jsonObj) {
		Message m = new Message();
		JSONObject object = jsonObj.isObject();
		m.id = (int)object.get("id").isNumber().doubleValue();
		m.from = object.get("from").isString().stringValue();
		m.to = object.get("to").isString().stringValue();
		m.content = object.get("content").isString().stringValue();
		JSONArray array = object.get("params").isArray();
		String[] params = new String[array.size()];
		for (int i = 0; i < params.length; i++) {
			params[i] = array.get(i).isString().stringValue();
		}
		m.params = params;
		return m;
	}

	
	
}
